/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.serviceimpl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import com.sesame.dao.AdminDAO;
import com.sesame.dao.AdminLogDAO;
import com.sesame.dao.AlbumDAO;
import com.sesame.dao.ComplaintDAO;
import com.sesame.dao.FriendDAO;
import com.sesame.dao.NoticeDAO;
import com.sesame.dao.UserDAO;
import com.sesame.domain.Admin;
import com.sesame.domain.AdminLog;
import com.sesame.domain.Album;
import com.sesame.domain.Complaint;
import com.sesame.domain.Notice;
import com.sesame.domain.User;
import com.sesame.service.AdminService;
import com.sesame.service.AlbumService;
import com.sesame.service.SearchService;

/**
 * @author voidman
 * 
 */
public class AdminServiceImpl implements AdminService {

    private AdminDAO adminDAO;
    private Admin admin;
    private Complaint complaint;
    private NoticeDAO noticeDAO;
    private ComplaintDAO complaintDAO;
    private AdminLogDAO adminLogDAO;
    private UserDAO userDAO;
    private AlbumDAO albumDAO;
    private AlbumService albumService;
    private SearchService searchService;
    private FriendDAO friendDAO;

    public void setAdminDAO(AdminDAO adminDAO) {
        this.adminDAO = adminDAO;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public void setComplaint(Complaint complaint) {
        this.complaint = complaint;
    }

    public void setNoticeDAO(NoticeDAO noticeDAO) {
        this.noticeDAO = noticeDAO;
    }

    public void setComplaintDAO(ComplaintDAO complaintDAO) {
        this.complaintDAO = complaintDAO;
    }

    public void setAdminLogDAO(AdminLogDAO adminLogDAO) {
        this.adminLogDAO = adminLogDAO;
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setAlbumDAO(AlbumDAO albumDAO) {
        this.albumDAO = albumDAO;
    }

    public void setAlbumService(AlbumService albumService) {
        this.albumService = albumService;
    }

    public void setSearchService(SearchService searchService) {
        this.searchService = searchService;
    }

    public void setFriendDAO(FriendDAO friendDAO) {
        this.friendDAO = friendDAO;
    }

    public boolean isLogin(HttpServletRequest request, String userName,
            String password, Map model) {
        Admin admin = adminDAO.getAdminByUserName(userName);
        if (admin == null || !admin.getPassword().equals(md5(password))) {
            model.put("error_admin", "该管理员不存在或密码错误！");
            return false;
        } else {
            request.getSession().setAttribute("adminId", admin.getId());
            request.getSession().setAttribute("adminUserName",
                    admin.getUserName());
            request.getSession().setAttribute("adminRank", admin.getRank());
            return true;
        }
    }

    public boolean validatePassword(Admin admin, String confirmPassword,
            Map model) {
        if (confirmPassword.equals(admin.getPassword())) {
            return true;
        } else {
            model.put("error_password", "两次密码输入不一致！");
            return false;
        }
    }

    public boolean isNewAdmin(Admin admin, Map model) {
        Admin DBadmin = adminDAO.getAdminByUserName(admin.getUserName());

        if (DBadmin == null) {
            md5(admin);
            adminDAO.addAdmin(admin);
            return true;
        } else {
            model.put("error_userName", "该帐号已经存在！");
            return false;
        }
    }

    public List<Admin> listAllAdmin() {
        return adminDAO.getAllAdmin();

    }

    public void deleteAdmin(Integer id) {
        adminDAO.deleteAdminById(id);

    }

    public void resetPassword(Integer id) {
        admin.setId(id);
        md5(admin);
        admin.setPassword("111111");
        adminDAO.updateAdminPassword(admin);

    }

    public List<Notice> listAllNotice() {
        return noticeDAO.getAllNotice();
    }

    public boolean isNewNotice(Notice notice, Map model) {
        Notice DBnotice = noticeDAO.getNoticeByTitle(notice.getTitle());
        if (DBnotice == null) {
            noticeDAO.addNotice(notice);
            return true;
        } else {
            model.put("error_notice_title", "该标题已存在！");
            return false;
        }
    }

    public void deleteNotice(Integer id) {
        noticeDAO.deleteNoticeById(id);
    }

    public Notice showNotice(Integer id) {
        return noticeDAO.getNoticeById(id);
    }

    public boolean isEditNotice(Notice notice, Map model) {

        if (noticeDAO.getNoticeById(notice.getId()).getTitle().equals(
                notice.getTitle())) {
            noticeDAO.updateNotice(notice);
            return true;
        } else if (noticeDAO.getNoticeByTitle(notice.getTitle()) == null) {
            noticeDAO.updateNotice(notice);
            return true;
        } else {
            model.put("error_notice_title", "该标题已经存在！");
            return false;
        }

    }

    public List<Complaint> listAllComplaint() {
        return complaintDAO.getAllComplaint();
    }

    public void deleteComplaint(Integer id) {
        complaintDAO.deleteComplaintById(id);

    }

    public void editComplaintNote(Integer id, String note) {
        complaint.setId(id);
        complaint.setNote(note);
        complaintDAO.updateComplaintNote(complaint);

    }

    public void dealComplaint(Integer id, String state) {
        complaint.setId(id);
        complaint.setState(state);
        complaintDAO.updateComplaintState(complaint);

    }

    public boolean modifyPassword(String oldPassword, Admin admin, Map model) {

        Admin dbAdmin = adminDAO.getAdminById(admin.getId());
        if (dbAdmin.getPassword().equals(md5(oldPassword))) {
            md5(admin);
            adminDAO.updateAdminPassword(admin);
            return true;
        } else {
            model.put("error_oldPassword", "原密码不正确！");
            return false;
        }
    }

    public List<AdminLog> listAllAdminLog() {
        return adminLogDAO.getAllAdminLog();

    }

    public List<User> listAllUser() {
        return userDAO.getAllUser();
    }

    public void deleteUser(Integer id) {
        User user = userDAO.getUserById(id);

        searchService.unindex(user);
        friendDAO.deleteFriendByReceiverId(id);
        List<Album> albumList = albumDAO.getAlbumByUserId(id);

        for (Album album : albumList)
            albumService.deleteAlbum(album.getId());
        userDAO.deleteUserById(id);
    }

    private Admin md5(Admin admin) {
        Md5PasswordEncoder md5 = new Md5PasswordEncoder();
        admin.setPassword(md5.encodePassword(admin.getPassword(), new String(
                "canyouguessme")));
        return admin;
    }

    private String md5(String password) {
        Md5PasswordEncoder md5 = new Md5PasswordEncoder();
        password = md5.encodePassword(password, new String("canyouguessme"));
        return password;
    }
}